package com.rain.cy.framework.utils;

import org.dozer.DozerBeanMapperBuilder;
import org.dozer.Mapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author chen_yu
 * @date 2020年7月22日14:23:30
 * 对象属性转换工具
 */
public class BeanMapper {

    private static final Mapper MAPPER = DozerBeanMapperBuilder.buildDefault();

    /**
     * 对象和类进行映射 做转换
     *
     * @param source           源对象
     * @param destinationClass 目标类
     * @param <T>              泛型
     * @return 返回要转换的类型
     */
    public static <T> T map(Object source, Class<T> destinationClass) {
        if (source == null) {
            return null;
        }
        return MAPPER.map(source, destinationClass);
    }

    public static void map(Object source, Object destination) {
        MAPPER.map(source, destination);
    }

    public static <T> List<T> mapList(Collection<T> sourceList, Class<T> destinationClass) {
        List<T> destinationList = new ArrayList<>();
        for (Object sourceObject : sourceList) {
            destinationList.add(MAPPER.map(sourceObject, destinationClass));
        }
        return destinationList;
    }

}
